package com.lin.cms.service.impl;

import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lin.cms.util.Paging;
import com.lin.cms.criteria.FileCriteria;
import com.lin.cms.domain.File;
import com.lin.cms.service.FileService;
import com.lin.cms.dao.FileMapper;

@Service
public class FileServiceImpl implements FileService{
	
	@Autowired
    private FileMapper fileMapper;
    
	public File selectByPrimaryKey(String filePath){
		return fileMapper.selectByPrimaryKey(filePath);
	}
	
	public List<File> selectByCriteria(FileCriteria criteria){
		return fileMapper.selectByCriteria(criteria);
	}
	
	public int deleteByPrimaryKey(String filePath){
		int actualRowsAffected = 0;
		
		File _oldFile = fileMapper.selectByPrimaryKey(filePath);
		
		if (_oldFile != null) {
			actualRowsAffected = fileMapper.deleteByPrimaryKey(filePath);
		}
		
		return actualRowsAffected;
	}
	
	public int deleteByCriteria(FileCriteria criteria){
		int actualRowsAffected = 0;
		
		List<File> _oldFile = fileMapper.selectByCriteria(criteria);
		
		if (_oldFile.size() != 0) {
			actualRowsAffected = fileMapper.deleteByCriteria(criteria);
		}
		
		return actualRowsAffected;
	}
	
	public int insert(File file){
		return fileMapper.insert(file);
	}
	
	public int updateByPrimaryKeySelective(File file){
		int actualRowsAffected = 0;
		
		String advertisingId = file.getFilePath();

		File _oldFile = fileMapper.selectByPrimaryKey(advertisingId);
		
		if (_oldFile != null) {
			actualRowsAffected = fileMapper.updateByPrimaryKeySelective(file);
		}
		
		return actualRowsAffected;
	}
	
	public int updateByPrimaryKey(File file){
		int actualRowsAffected = 0;
		
		String advertisingId = file.getFilePath();

		File _oldFile = fileMapper.selectByPrimaryKey(advertisingId);
		
		if (_oldFile != null) {
			actualRowsAffected = fileMapper.updateByPrimaryKey(file);
		}
		
		return actualRowsAffected;
	}
	
	public List<File> selectByPage(FileCriteria criteria) {
		int totalResults = fileMapper.countByCriteria(criteria);
		Paging paging = criteria.getPaging();
		paging.setTotalResults(totalResults);
		
		return fileMapper.selectByPage(criteria,new RowBounds(paging.getFirstResult(),paging.getMaxResults()));
	}
	
	public int countByCriteria(FileCriteria criteria){
		return fileMapper.countByCriteria(criteria);
	}

	public void saveFileByPath(String filepath, String name, Boolean valid) {
		// TODO Auto-generated method stub
		File attFile=new File();
		attFile.setFilePath(filepath);
		attFile.setFileName(name);
		attFile.setFileIsvalid(valid);
		fileMapper.insert(attFile);
	}
	
}
